package command;

import entity.Student;

import java.util.List;
import java.util.Scanner;

public class DeleteCommand implements Command{

    private final Scanner scanner = new Scanner(System.in);

    private Student _student = new Student();
    @Override
    public void execute(List<Student> students) throws InterruptedException {
        int id;
        while(true){
            try{
                System.out.print("请输入要删除的学生的学号：");
                String line = scanner.nextLine();
                id = Integer.parseInt(line);
                break;
            }catch(Exception e){
                System.err.println("请输入一个数字！");
                Thread.sleep(100);
            }
        }

        for(Student student : students){
            if(student.getId() == id){
                students.remove(student);
                _student = student;
                return;
            }
        }

        System.err.println("没有找到对应的学生！");
    }

    @Override
    public void undo(List<Student> students) {
        students.add(_student);
        System.out.println("成功撤销删除操作！添加的学生信息是:"+_student);
    }
}
